package com.cnbbx.office.Controller;

import com.artofsolving.jodconverter.DocumentFamily;
import com.artofsolving.jodconverter.DocumentFormat;
import com.cnbbx.office.Util.DocumentUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import java.io.BufferedInputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;

import static com.cnbbx.office.Controller.BaseController.error;

@RestController
public class MainController {

    @RequestMapping(method = RequestMethod.GET, value = "/api/index")
    public String index(@RequestParam(value = "name", defaultValue = "Jinge") String name) {
        return "hello " + name + "!";
    }

    @RequestMapping(method = RequestMethod.GET, value = "/api/show")
    public Object show(@RequestParam(value = "file", defaultValue = "http://192.168.0.3:9555/file/down/be65dcf5eb1722e6668c5b1f2da8754c") String url, final HttpServletResponse response) {
        try {
            //创建url;
            URL urlStr = new URL(url);
            //创建url连接;
            HttpURLConnection urlconn = (HttpURLConnection) urlStr.openConnection();
            //链接远程服务器;
            urlconn.connect();
            final DocumentFormat pdf = new DocumentFormat("Portable Document Format", "application/pdf", "pdf");
            pdf.setExportFilter(DocumentFamily.DRAWING, "draw_pdf_Export");
            pdf.setExportFilter(DocumentFamily.PRESENTATION, "impress_pdf_Export");
            pdf.setExportFilter(DocumentFamily.SPREADSHEET, "calc_pdf_Export");
            pdf.setExportFilter(DocumentFamily.TEXT, "writer_pdf_Export");
            final DocumentFormat xls = new DocumentFormat("Microsoft Excel", DocumentFamily.SPREADSHEET, "application/vnd.ms-excel", "xls");
            xls.setExportFilter(DocumentFamily.SPREADSHEET, "MS Excel 97");
            final DocumentFormat xlsx = new DocumentFormat("Microsoft Excel 2007 XML", DocumentFamily.SPREADSHEET, "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet", "xlsx");
            xlsx.setExportFilter(DocumentFamily.SPREADSHEET, "MS Excel 2007");
            final DocumentFormat ppt = new DocumentFormat("Microsoft PowerPoint", DocumentFamily.PRESENTATION, "application/vnd.ms-powerpoint", "ppt");
            ppt.setExportFilter(DocumentFamily.PRESENTATION, "MS PowerPoint 97");
            final DocumentFormat pptx = new DocumentFormat("Microsoft PowerPoint 2007 XML", DocumentFamily.PRESENTATION, "application/vnd.openxmlformats-officedocument.presentationml.presentation", "pptx");
            pptx.setExportFilter(DocumentFamily.PRESENTATION, "MS PowerPoint 2007");
            final DocumentFormat doc = new DocumentFormat("Microsoft Word", DocumentFamily.TEXT, "application/msword", "doc");
            doc.setExportFilter(DocumentFamily.TEXT, "MS Word 97");
            final DocumentFormat docx = new DocumentFormat("Microsoft Word 2007 XML", DocumentFamily.TEXT, "application/vnd.openxmlformats-officedocument.wordprocessingml.document", "docx");
            docx.setExportFilter(DocumentFamily.TEXT, "MS Word 2007");
            BufferedInputStream bis = null;
            try {
                //获取远程服务器端文件输入流;
                bis = new BufferedInputStream(urlconn.getInputStream());
                DocumentFormat autoDoc = xls;
                System.out.println("file type: " + urlconn.getContentType());
                switch (urlconn.getContentType()) {
                    case "application/vnd.ms-excel":
                        autoDoc = xls;
                        break;
                    case "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet":
                        autoDoc = xlsx;
                        break;
                    case "application/vnd.ms-powerpoint":
                        autoDoc = ppt;
                        break;
                    case "application/vnd.openxmlformats-officedocument.presentationml.presentation":
                        autoDoc = pptx;
                        break;
                    case "application/msword":
                        autoDoc = doc;
                        break;
                    case "application/vnd.openxmlformats-officedocument.wordprocessingml.document":
                        autoDoc = docx;
                        break;
                }
                OutputStream os = response.getOutputStream();
                DocumentUtils.convert(bis, autoDoc, os, pdf);
                os.flush();
                os.close();
            } catch (Exception e) {
                return error("Error 500", "糟糕，您访问的OFFICE文档预览失败。。。");
            } finally {
                if (bis != null) { //关闭文件输入流;
                    bis.close();
                    urlconn.disconnect();
                }
            }
        } catch (IOException e) {
            return error("Error 500", "糟糕，您访问的OSS资源服务繁忙。。。");
        }
        return null;
    }

}
